home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / clang / gnumake.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1994-05-21  |  16KB  |  723 lines

  1. /* Miscellaneous generic support functions for GNU Make.
  2. Copyright (C) 1988, 89, 90, 91, 92, 93, 94 Free Software Foundation, Inc.
  3. This file is part of GNU Make.
  4.  
  5. GNU Make is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9.  
  10. GNU Make is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with GNU Make; see the file COPYING.  If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19. #include "make.h"
  20. #include "dep.h"
  21.  
  22.  
  23. /* Compare strings *S1 and *S2.
  24.    Return negative if the first is less, positive if it is greater,
  25.    zero if they are equal.  */
  26.  
  27. int
  28. alpha_compare (s1, s2)
  29.      char **s1, **s2;
  30. {
  31.   if (**s1 != **s2)
  32.     return **s1 - **s2;
  33.   return strcmp (*s1, *s2);
  34. }
  35.  
  36. /* Discard each backslash-newline combination from LINE.
  37.    Backslash-backslash-newline combinations become backslash-newlines.
  38.    This is done by copying the text at LINE into itself.  */
  39.  
  40. void
  41. collapse_continuations (line)
  42.      char *line;
  43. {
  44.   register char *in, *out, *p;
  45.   register int backslash;
  46.   register unsigned int bs_write;
  47.  
  48.   in = index (line, '\n');
  49.   if (in == 0)
  50.     return;
  51.  
  52.   out = in;
  53.   if (out > line)
  54.     while (out[-1] == '\\')
  55.       --out;
  56.  
  57.   while (*in != '\0')
  58.     {
  59.       /* BS_WRITE gets the number of quoted backslashes at
  60.      the end just before IN, and BACKSLASH gets nonzero
  61.      if the next character is quoted.  */
  62.       backslash = 0;
  63.       bs_write = 0;
  64.       for (p = in - 1; p >= line && *p == '\\'; --p)
  65.     {
  66.       if (backslash)
  67.         ++bs_write;
  68.       backslash = !backslash;
  69.  
  70.       /* It should be impossible to go back this far without exiting,
  71.          but if we do, we can't get the right answer.  */
  72.       if (in == out - 1)
  73.         abort ();
  74.     }
  75.  
  76.       /* Output the appropriate number of backslashes.  */
  77.       while (bs_write-- > 0)
  78.     *out++ = '\\';
  79.  
  80.       /* Skip the newline.  */
  81.       ++in;
  82.  
  83.       /* If the newline is quoted, discard following whitespace
  84.      and any preceding whitespace; leave just one space.  */
  85.       if (backslash)
  86.     {
  87.       in = next_token (in);
  88.       while (out > line && isblank (out[-1]))
  89.         --out;
  90.       *out++ = ' ';
  91.     }
  92.       else
  93.     /* If the newline isn't quoted, put it in the output.  */
  94.     *out++ = '\n';
  95.  
  96.       /* Now copy the following line to the output.
  97.      Stop when we find backslashes followed by a newline.  */
  98.       while (*in != '\0')
  99.     if (*in == '\\')
  100.       {
  101.         p = in + 1;
  102.         while (*p == '\\')
  103.           ++p;
  104.         if (*p == '\n')
  105.           {
  106.         in = p;
  107.         break;
  108.           }
  109.         while (in < p)
  110.           *out++ = *in++;
  111.       }
  112.     else
  113.       *out++ = *in++;
  114.     }
  115.  
  116.   *out = '\0';
  117. }
  118.  
  119.  
  120. /* Remove comments from LINE.
  121.    This is done by copying the text at LINE onto itself.  */
  122.  
  123. void
  124. remove_comments (line)
  125.      char *line;
  126. {
  127.   char *comment;
  128.  
  129.   comment = find_char_unquote (line, '#', 0);
  130.  
  131.   if (comment != 0)
  132.     /* Cut off the line at the #.  */
  133.     *comment = '\0';
  134. }
  135.  
  136. /* Print N spaces (used by DEBUGPR for target-depth).  */
  137.  
  138. void
  139. print_spaces (n)
  140.      register unsigned int n;
  141. {
  142.   while (n-- > 0)
  143.     putchar (' ');
  144. }
  145.  
  146.  
  147. /* Return a newly-allocated string whose contents
  148.    concatenate those of s1, s2, s3.  */
  149.  
  150. char *
  151. concat (s1, s2, s3)
  152.      register char *s1, *s2, *s3;
  153. {
  154.   register unsigned int len1, len2, len3;
  155.   register char *result;
  156.  
  157.   len1 = *s1 != '\0' ? strlen (s1) : 0;
  158.   len2 = *s2 != '\0' ? strlen (s2) : 0;
  159.   len3 = *s3 != '\0' ? strlen (s3) : 0;
  160.  
  161.   result = (char *) xmalloc (len1 + len2 + len3 + 1);
  162.  
  163.   if (*s1 != '\0')
  164.     bcopy (s1, result, len1);
  165.   if (*s2 != '\0')
  166.     bcopy (s2, result + len1, len2);
  167.   if (*s3 != '\0')
  168.     bcopy (s3, result + len1 + len2, len3);
  169.   *(result + len1 + len2 + len3) = '\0';
  170.  
  171.   return result;
  172. }
  173.  
  174. /* Print a message on stdout.  */
  175.  
  176. void
  177. message (s1, s2, s3, s4, s5, s6)
  178.      char *s1, *s2, *s3, *s4, *s5, *s6;
  179. {
  180.   if (makelevel == 0)
  181.     printf ("%s: ", program);
  182.   else
  183.     printf ("%s[%u]: ", program, makelevel);
  184.   printf (s1, s2, s3, s4, s5, s6);
  185.   putchar ('\n');
  186.   fflush (stdout);
  187. }
  188.  
  189. /* Print an error message and exit.  */
  190.  
  191. /* VARARGS1 */
  192. void
  193. fatal (s1, s2, s3, s4, s5, s6)
  194.      char *s1, *s2, *s3, *s4, *s5, *s6;
  195. {
  196.   if (makelevel == 0)
  197.     fprintf (stderr, "%s: *** ", program);
  198.   else
  199.     fprintf (stderr, "%s[%u]: *** ", program, makelevel);
  200.   fprintf (stderr, s1, s2, s3, s4, s5, s6);
  201.   fputs (".  Stop.\n", stderr);
  202.  
  203.   die (2);
  204. }
  205.  
  206. /* Print error message.  `s1' is printf control string, `s2' is arg for it. */
  207.  
  208. /* VARARGS1 */
  209.  
  210. void
  211. error (s1, s2, s3, s4, s5, s6)
  212.      char *s1, *s2, *s3, *s4, *s5, *s6;
  213. {
  214.   if (makelevel == 0)
  215.     fprintf (stderr, "%s: ", program);
  216.   else
  217.     fprintf (stderr, "%s[%u]: ", program, makelevel);
  218.   fprintf (stderr, s1, s2, s3, s4, s5, s6);
  219.   putc ('\n', stderr);
  220.   fflush (stderr);
  221. }
  222.  
  223. void
  224. makefile_error (file, lineno, s1, s2, s3, s4, s5, s6)
  225.      char *file;
  226.      unsigned int lineno;
  227.      char *s1, *s2, *s3, *s4, *s5, *s6;
  228. {
  229.   fprintf (stderr, "%s:%u: ", file, lineno);
  230.   fprintf (stderr, s1, s2, s3, s4, s5, s6);
  231.   putc ('\n', stderr);
  232.   fflush (stderr);
  233. }
  234.  
  235. void
  236. makefile_fatal (file, lineno, s1, s2, s3, s4, s5, s6)
  237.      char *file;
  238.      unsigned int lineno;
  239.      char *s1, *s2, *s3, *s4, *s5, *s6;
  240. {
  241.   fprintf (stderr, "%s:%u: *** ", file, lineno);
  242.   fprintf (stderr, s1, s2, s3, s4, s5, s6);
  243.   fputs (".  Stop.\n", stderr);
  244.  
  245.   die (2);
  246. }
  247.  
  248. #ifndef HAVE_STRERROR
  249.  
  250. #undef    strerror
  251.  
  252. char *
  253. strerror (errnum)
  254.      int errnum;
  255. {
  256.   extern int errno, sys_nerr;
  257.   extern char *sys_errlist[];
  258.   static char buf[] = "Unknown error 12345678901234567890";
  259.  
  260.   if (errno < sys_nerr)
  261.     return sys_errlist[errnum];
  262.  
  263.   sprintf ("Unknown error %d", buf, errnum);
  264.   return buf;
  265. }
  266. #endif
  267.  
  268. /* Print an error message from errno.  */
  269.  
  270. void
  271. perror_with_name (str, name)
  272.      char *str, *name;
  273. {
  274.   error ("%s%s: %s", str, name, strerror (errno));
  275. }
  276.  
  277. /* Print an error message from errno and exit.  */
  278.  
  279. void
  280. pfatal_with_name (name)
  281.      char *name;
  282. {
  283.   fatal ("%s: %s", name, strerror (errno));
  284.  
  285.   /* NOTREACHED */
  286. }
  287.  
  288. /* Like malloc but get fatal error if memory is exhausted.  */
  289.  
  290. #undef xmalloc
  291. #undef xrealloc
  292.  
  293. char *
  294. xmalloc (size)
  295.      unsigned int size;
  296. {
  297.   char *result = (char *) malloc (size);
  298.   if (result == 0)
  299.     fatal ("virtual memory exhausted");
  300.   return result;
  301. }
  302.  
  303.  
  304. char *
  305. xrealloc (ptr, size)
  306.      char *ptr;
  307.      unsigned int size;
  308. {
  309.   char *result = (char *) realloc (ptr, size);
  310.   if (result == 0)
  311.     fatal ("virtual memory exhausted");
  312.   return result;
  313. }
  314.  
  315. char *
  316. savestring (str, length)
  317.      char *str;
  318.      unsigned int length;
  319. {
  320.   register char *out = (char *) xmalloc (length + 1);
  321.   if (length > 0)
  322.     bcopy (str, out, length);
  323.   out[length] = '\0';
  324.   return out;
  325. }
  326.  
  327. /* Search string BIG (length BLEN) for an occurrence of
  328.    string SMALL (length SLEN).  Return a pointer to the
  329.    beginning of the first occurrence, or return nil if none found.  */
  330.  
  331. char *
  332. sindex (big, blen, small, slen)
  333.      char *big;
  334.      unsigned int blen;
  335.      char *small;
  336.      unsigned int slen;
  337. {
  338.   register unsigned int b;
  339.  
  340.   if (blen < 1)
  341.     blen = strlen (big);
  342.   if (slen < 1)
  343.     slen = strlen (small);
  344.  
  345.   for (b = 0; b < blen; ++b)
  346.     if (big[b] == *small && !strncmp (&big[b + 1], small + 1, slen - 1))
  347.       return (&big[b]);
  348.  
  349.   return 0;
  350. }
  351.  
  352. /* Limited INDEX:
  353.    Search through the string STRING, which ends at LIMIT, for the character C.
  354.    Returns a pointer to the first occurrence, or nil if none is found.
  355.    Like INDEX except that the string searched ends where specified
  356.    instead of at the first null.  */
  357.  
  358. char *
  359. lindex (s, limit, c)
  360.      register char *s, *limit;
  361.      int c;
  362. {
  363.   while (s < limit)
  364.     if (*s++ == c)
  365.       return s - 1;
  366.  
  367.   return 0;
  368. }
  369.  
  370. /* Return the address of the first whitespace or null in the string S.  */
  371.  
  372. char *
  373. end_of_token (s)
  374.      char *s;
  375. {
  376.   while (*s != '\0' && !isblank (*s))
  377.     ++s;
  378.   return s;
  379. }
  380.  
  381. /* Return the address of the first nonwhitespace or null in the string S.  */
  382.  
  383. char *
  384. next_token (s)
  385.      char *s;
  386. {
  387.   register char *p = s;
  388.  
  389.   while (isblank (*p))
  390.     ++p;
  391.   return p;
  392. }
  393.  
  394. /* Find the next token in PTR; return the address of it, and store the
  395.    length of the token into *LENGTHPTR if LENGTHPTR is not nil.  */
  396.  
  397. char *
  398. find_next_token (ptr, lengthptr)
  399.      char **ptr;
  400.      unsigned int *lengthptr;
  401. {
  402.   char *p = next_token (*ptr);
  403.   char *end;
  404.  
  405.   if (*p == '\0')
  406.     return 0;
  407.  
  408.   *ptr = end = end_of_token (p);
  409.   if (lengthptr != 0)
  410.     *lengthptr = end - p;
  411.   return p;
  412. }
  413.  
  414. /* Copy a chain of `struct dep', making a new chain
  415.    with the same contents as the old one.  */
  416.  
  417. struct dep *
  418. copy_dep_chain (d)
  419.      register struct dep *d;
  420. {
  421.   register struct dep *c;
  422.   struct dep *firstnew = 0;
  423.   struct dep *lastnew;
  424.  
  425.   while (d != 0)
  426.     {
  427.       c = (struct dep *) xmalloc (sizeof (struct dep));
  428.       bcopy ((char *) d, (char *) c, sizeof (struct dep));
  429.       if (c->name != 0)
  430.     c->name = savestring (c->name, strlen (c->name));
  431.       c->next = 0;
  432.       if (firstnew == 0)
  433.     firstnew = lastnew = c;
  434.       else
  435.     lastnew = lastnew->next = c;
  436.  
  437.       d = d->next;
  438.     }
  439.  
  440.   return firstnew;
  441. }
  442.  
  443. #ifdef    iAPX286
  444. /* The losing compiler on this machine can't handle this macro.  */
  445.  
  446. char *
  447. dep_name (dep)
  448.      struct dep *dep;
  449. {
  450.   return dep->name == 0 ? dep->file->name : dep->name;
  451. }
  452. #endif
  453.  
  454. #ifdef    GETLOADAVG_PRIVILEGED
  455.  
  456. #ifdef POSIX
  457.  
  458. /* Hopefully if a system says it's POSIX.1 and has the setuid and setgid
  459.    functions, they work as POSIX.1 says.  Some systems (Alpha OSF/1 1.2,
  460.    for example) which claim to be POSIX.1 also have the BSD setreuid and
  461.    setregid functions, but they don't work as in BSD and only the POSIX.1
  462.    way works.  */
  463.  
  464. #ifndef HAVE_SETREUID
  465. #undef HAVE_SETREUID
  466. #endif
  467. #ifndef HAVE_SETREGID
  468. #undef HAVE_SETREGID
  469. #endif
  470.  
  471. #else    /* Not POSIX.  */
  472.  
  473. /* Some POSIX.1 systems have the seteuid and setegid functions.  In a
  474.    POSIX-like system, they are the best thing to use.  However, some
  475.    non-POSIX systems have them too but they do not work in the POSIX style
  476.    and we must use setreuid and setregid instead.  */
  477.  
  478. #undef HAVE_SETEUID
  479. #undef HAVE_SETEGID
  480.  
  481. #endif    /* POSIX.  */
  482.  
  483. #ifndef    HAVE_UNISTD_H
  484. extern int getuid (), getgid (), geteuid (), getegid ();
  485. extern int setuid (), setgid ();
  486. #ifdef HAVE_SETEUID
  487. extern int seteuid ();
  488. #else
  489. #ifdef    HAVE_SETREUID
  490. extern int setreuid ();
  491. #endif    /* Have setreuid.  */
  492. #endif    /* Have seteuid.  */
  493. #ifdef HAVE_SETEGID
  494. extern int setegid ();
  495. #else
  496. #ifdef    HAVE_SETREGID
  497. extern int setregid ();
  498. #endif    /* Have setregid.  */
  499. #endif    /* Have setegid.  */
  500. #endif    /* No <unistd.h>.  */
  501.  
  502. /* Keep track of the user and group IDs for user- and make- access.  */
  503. static int user_uid = -1, user_gid = -1, make_uid = -1, make_gid = -1;
  504. #define    access_inited    (user_uid != -1)
  505. static enum { make, user } current_access;
  506.  
  507.  
  508. /* Under -d, write a message describing the current IDs.  */
  509.  
  510. static void
  511. log_access (flavor)
  512.      char *flavor;
  513. {
  514.   if (! debug_flag)
  515.     return;
  516.  
  517.   /* All the other debugging messages go to stdout,
  518.      but we write this one to stderr because it might be
  519.      run in a child fork whose stdout is piped.  */
  520.  
  521.   fprintf (stderr, "%s access: user %d (real %d), group %d (real %d)\n",
  522.        flavor, geteuid (), getuid (), getegid (), getgid ());
  523.   fflush (stderr);
  524. }
  525.  
  526.  
  527. static void
  528. init_access ()
  529. {
  530.   user_uid = getuid ();
  531.   user_gid = getgid ();
  532.  
  533.   make_uid = geteuid ();
  534.   make_gid = getegid ();
  535.  
  536.   /* Do these ever fail?  */
  537.   if (user_uid == -1 || user_gid == -1 || make_uid == -1 || make_gid == -1)
  538.     pfatal_with_name ("get{e}[gu]id");
  539.  
  540.   log_access ("Initialized");
  541.  
  542.   current_access = make;
  543. }
  544.  
  545. #endif    /* GETLOADAVG_PRIVILEGED */
  546.  
  547. /* Give the process appropriate permissions for access to
  548.    user data (i.e., to stat files, or to spawn a child process).  */
  549. void
  550. user_access ()
  551. {
  552. #ifdef    GETLOADAVG_PRIVILEGED
  553.  
  554.   if (!access_inited)
  555.     init_access ();
  556.  
  557.   if (current_access == user)
  558.     return;
  559.  
  560.   /* We are in "make access" mode.  This means that the effective user and
  561.      group IDs are those of make (if it was installed setuid or setgid).
  562.      We now want to set the effective user and group IDs to the real IDs,
  563.      which are the IDs of the process that exec'd make.  */
  564.  
  565. #ifdef    HAVE_SETEUID
  566.  
  567.   /* Modern systems have the seteuid/setegid calls which set only the
  568.      effective IDs, which is ideal.  */
  569.  
  570.   if (seteuid (user_uid) < 0)
  571.     pfatal_with_name ("user_access: seteuid");
  572.  
  573. #else    /* Not HAVE_SETEUID.  */
  574.  
  575. #ifndef    HAVE_SETREUID
  576.  
  577.   /* System V has only the setuid/setgid calls to set user/group IDs.
  578.      There is an effective ID, which can be set by setuid/setgid.
  579.      It can be set (unless you are root) only to either what it already is
  580.      (returned by geteuid/getegid, now in make_uid/make_gid),
  581.      the real ID (return by getuid/getgid, now in user_uid/user_gid),
  582.      or the saved set ID (what the effective ID was before this set-ID
  583.      executable (make) was exec'd).  */
  584.  
  585.   if (setuid (user_uid) < 0)
  586.     pfatal_with_name ("user_access: setuid");
  587.  
  588. #else    /* HAVE_SETREUID.  */
  589.  
  590.   /* In 4BSD, the setreuid/setregid calls set both the real and effective IDs.
  591.      They may be set to themselves or each other.  So you have two alternatives
  592.      at any one time.  If you use setuid/setgid, the effective will be set to
  593.      the real, leaving only one alternative.  Using setreuid/setregid, however,
  594.      you can toggle between your two alternatives by swapping the values in a
  595.      single setreuid or setregid call.  */
  596.  
  597.   if (setreuid (make_uid, user_uid) < 0)
  598.     pfatal_with_name ("user_access: setreuid");
  599.  
  600. #endif    /* Not HAVE_SETREUID.  */
  601. #endif    /* HAVE_SETEUID.  */
  602.  
  603. #ifdef    HAVE_SETEGID
  604.   if (setegid (user_gid) < 0)
  605.     pfatal_with_name ("user_access: setegid");
  606. #else
  607. #ifndef    HAVE_SETREGID
  608.   if (setgid (user_gid) < 0)
  609.     pfatal_with_name ("user_access: setgid");
  610. #else
  611.   if (setregid (make_gid, user_gid) < 0)
  612.     pfatal_with_name ("user_access: setregid");
  613. #endif
  614. #endif
  615.  
  616.   current_access = user;
  617.  
  618.   log_access ("User");
  619.  
  620. #endif    /* GETLOADAVG_PRIVILEGED */
  621. }
  622.  
  623. /* Give the process appropriate permissions for access to
  624.    make data (i.e., the load average).  */
  625. void
  626. make_access ()
  627. {
  628. #ifdef    GETLOADAVG_PRIVILEGED
  629.  
  630.   if (!access_inited)
  631.     init_access ();
  632.  
  633.   if (current_access == make)
  634.     return;
  635.  
  636.   /* See comments in user_access, above.  */
  637.  
  638. #ifdef    HAVE_SETEUID
  639.   if (seteuid (make_uid) < 0)
  640.     pfatal_with_name ("make_access: seteuid");
  641. #else
  642. #ifndef    HAVE_SETREUID
  643.   if (setuid (make_uid) < 0)
  644.     pfatal_with_name ("make_access: setuid");
  645. #else
  646.   if (setreuid (user_uid, make_uid) < 0)
  647.     pfatal_with_name ("make_access: setreuid");
  648. #endif
  649. #endif
  650.  
  651. #ifdef    HAVE_SETEGID
  652.   if (setegid (make_gid) < 0)
  653.     pfatal_with_name ("make_access: setegid");
  654. #else
  655. #ifndef    HAVE_SETREGID
  656.   if (setgid (make_gid) < 0)
  657.     pfatal_with_name ("make_access: setgid");
  658. #else
  659.   if (setregid (user_gid, make_gid) < 0)
  660.     pfatal_with_name ("make_access: setregid");
  661. #endif
  662. #endif
  663.  
  664.   current_access = make;
  665.  
  666.   log_access ("Make");
  667.  
  668. #endif    /* GETLOADAVG_PRIVILEGED */
  669. }
  670.  
  671. /* Give the process appropriate permissions for a child process.
  672.    This is like user_access, but you can't get back to make_access.  */
  673. void
  674. child_access ()
  675. {
  676. #ifdef    GETLOADAVG_PRIVILEGED
  677.  
  678.   if (!access_inited)
  679.     abort ();
  680.  
  681.   /* Set both the real and effective UID and GID to the user's.
  682.      They cannot be changed back to make's.  */
  683.  
  684. #ifndef    HAVE_SETREUID
  685.   if (setuid (user_uid) < 0)
  686.     pfatal_with_name ("child_access: setuid");
  687. #else
  688.   if (setreuid (user_uid, user_uid) < 0)
  689.     pfatal_with_name ("child_access: setreuid");
  690. #endif
  691.  
  692. #ifndef    HAVE_SETREGID
  693.   if (setgid (user_gid) < 0)
  694.     pfatal_with_name ("child_access: setgid");
  695. #else
  696.   if (setregid (user_gid, user_gid) < 0)
  697.     pfatal_with_name ("child_access: setregid");
  698. #endif
  699.  
  700.   log_access ("Child");
  701.  
  702. #endif    /* GETLOADAVG_PRIVILEGED */
  703. }
  704.  
  705. #ifdef NEED_GET_PATH_MAX
  706. unsigned int
  707. get_path_max ()
  708. {
  709.   static unsigned int value;
  710.  
  711.   if (value == 0)
  712.     {
  713.       long int x = pathconf ("/", _PC_PATH_MAX);
  714.       if (x > 0)
  715.     value = x;
  716.       else
  717.     return MAXPATHLEN;
  718.     }
  719.  
  720.   return value;
  721. }
  722. #endif
  723.